﻿using Microsoft.EntityFrameworkCore;
using NetTopologySuite.Geometries;
using OSGeo.GDAL;
using RemoteSensingImageDirectServer.Models.Map.Cache;

namespace RemoteSensingImageDirectServer.Data
{
    public class RasterMapCacheContext : DbContext
    {
        /// <summary>
        /// 程序配置项
        /// </summary>
        private readonly IConfiguration _configuration;

        /// <summary>
        /// 单景影像缓存表
        /// </summary>
        public DbSet<SingleImageCache> TBSingleImages { get; set; }

        /// <summary>
        /// 联合影像缓存表
        /// </summary>
        public DbSet<GroupImageCache> TBGroupImages { get; set; }

        /// <summary>
        /// 联合影像缓存表
        /// </summary>
        public DbSet<GroupImageWarpCache> TBGroupImageWarp { get; set; }


        public RasterMapCacheContext(IConfiguration configuration, DbContextOptions<RasterMapCacheContext> options) : base(options)
        {
            this._configuration = configuration;
        }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{

        //}

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 在这里配置你的实体和空间索引
            if (_configuration.GetConnectionString("DataBaseType") == "POSTGIS")
            {
                modelBuilder.HasPostgresExtension("postgis"); // 确保PostGIS扩展被启用
            }

            ////设置主键和联合索引
            modelBuilder.Entity<SingleImageCache>().HasKey(item => new { item.Key });
            modelBuilder.Entity<SingleImageCache>().HasIndex(item => new { item.Filename, item.EPSG });

            //设置主键和联合索引
            modelBuilder.Entity<GroupImageCache>().HasKey(item => new { item.Key });
            modelBuilder.Entity<GroupImageCache>().HasIndex(item => new { item.GroupName, item.CreateBy });

            //设置主键
            modelBuilder.Entity<GroupImageWarpCache>().HasKey(item => new { item.Key });
        }
    }
}
